package booksys.student;




import booksys.common.Utils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.ibatis.session.SqlSession;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class StudentService {

    public Map<String, Object> getStudentList(QueryDto queryDto) {
        SqlSession sess = Utils.openSession();
        try {
            StudentDao dao = sess.getMapper(StudentDao.class);

            //执行映射器方法之前执行分页参数(页码，每页记录数)设置
            PageHelper.startPage(queryDto);


            List<Student> studentList = dao.findStudentList(queryDto);


            //执行映射器方法之后，立即执行PageInfo对象创建
            PageInfo<Student> pageInfo = new PageInfo<Student>(studentList);

            Map<String,Object> page = new HashMap<>();
            page.put("current",pageInfo.getPageNum());//当前页
            page.put("pageSize",pageInfo.getPageSize());//每页最大记录数
            page.put("total",pageInfo.getTotal());//总记录数
            page.put("pages",pageInfo.getPages());//总页数
            page.put("size",pageInfo.getSize());//当前页记录数
            page.put("list",pageInfo.getList());//当前页数据记录
            page.put("first",1);
            page.put("pre",pageInfo.getPrePage());
            page.put("next",pageInfo.getNextPage());
            page.put("last",pageInfo.getPages());


            sess.commit();

            return page;

        } catch (Exception e) {
            sess.rollback();
            System.out.println(e);
            throw new RuntimeException("加载学生信息失败！", e);
        } finally {
            sess.close();
        }
    }


}
